package com.ants.gateway.config;

import com.ants.gateway.handle.RestfulAccessDeniedHandler;
import com.ants.gateway.handle.RestfulAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
import org.springframework.security.oauth2.server.resource.authentication.ReactiveJwtAuthenticationConverterAdapter;
import org.springframework.security.web.server.SecurityWebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author 蚂蚁会花呗
 * @create 2025/5/28 15:32
 */
@Configuration
@EnableWebFluxSecurity
public class ResourceServerConfig {

    /***
     * 白名单,直接放行的
     */
    private final String[] permissiveUrlList = {
            "/img/**",
            "/upload/**",
            "/ants/login",
            "/ants/captcha",
            "/ants/send_captcha",
            "/logout",
            "/doc.html",
            "/swagger-resources",
            "/swagger-resources/configuration/ui",
            "/v2/api-docs",
            "/webjars/**",
            "/wx/auth/*",
            "/favicon.ico",
            "/websocket/**",
            "/oauth/**",
            "/ants-product-dev/product/testOauth2",
            "/ants-order-dev/order/hello"
    };


    @Autowired
    private RestfulAuthorizationManager restfulAuthorizationManager;


    /**
     * 未授权
     * @return
     */
    @Bean
    public RestfulAccessDeniedHandler restfulAccessDeniedHandler(){
        return new RestfulAccessDeniedHandler();
    }

    /**
     * 未认证
     * @return
     */
    @Bean
    public RestfulAuthenticationEntryPoint restfulAuthenticationEntryPoint(){
        return new RestfulAuthenticationEntryPoint();
    }


    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.oauth2ResourceServer().jwt()
                .jwtAuthenticationConverter(jwtAuthenticationConverter());
        http.authorizeExchange()
                .pathMatchers(permissiveUrlList).permitAll()//白名单配置
                .anyExchange().access(restfulAuthorizationManager)//鉴权管理器配置
                .and().exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler())//处理未授权
                .authenticationEntryPoint(restfulAuthenticationEntryPoint())//处理未认证
                .and().csrf().disable();
        return http.build();
    }

    @Bean
    public Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> jwtAuthenticationConverter() {
        JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
        //取消权限的前缀，默认会加上SCOPE_
        jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
        //从那个字段中获取权限
        jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("authorities");
        JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
        jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);
        return new ReactiveJwtAuthenticationConverterAdapter(jwtAuthenticationConverter);
    }


}
